package apptoko.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import apptoko.com.adapter.ErrorHandler;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.event.Event.GetFullItemInfoFail;
import apptoko.com.event.Event.GetFullItemInfoSuccessEvent;
import apptoko.com.event.Event.RequestFailEvent;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;

public class HandleExternalItemActivity extends Activity implements
		OnClickListener {

	private BackgroundTasks bgTask;
	private AQuery aq;
	String accessToken;
	String packageName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_broker);
		bgTask = new BackgroundTasks(this);
		aq = new AQuery(this);
		String param = null;
		EasyTracker.getInstance().setContext(this);
		Intent intent = getIntent();
		if (intent.getData() != null) {
			if(intent.getData().getScheme().equalsIgnoreCase("asvn")){
				if(intent.getData().getHost().equalsIgnoreCase("details")){
					packageName = intent.getData().getQueryParameter("id");
					accessToken = Utils.getStaticAccessToken(this);
					bgTask.getFullItemInfo(accessToken, "", packageName, "ads");
					aq.id(R.id.broker_prg).visible();
					aq.id(R.id.broker_retry).gone();
					aq.id(R.id.empty_view).gone();
				}
			} else {
				param = intent.getData().getLastPathSegment();
				if (param != null) {
					if (param.equalsIgnoreCase("details")) {
						packageName = intent.getData().getQueryParameter("id");
						accessToken = Utils.getStaticAccessToken(this);
						bgTask.getFullItemInfo(accessToken, "", packageName, "ads");
						aq.id(R.id.broker_prg).visible();
						aq.id(R.id.broker_retry).gone();
						aq.id(R.id.empty_view).gone();
					} else {
						aq.id(R.id.broker_prg).gone();
						aq.id(R.id.broker_retry).gone();
						aq.id(R.id.empty_view).visible();
					}
				} else {
					aq.id(R.id.broker_prg).gone();
					aq.id(R.id.broker_retry).gone();
					aq.id(R.id.empty_view).visible();
				}
			}
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		if(intent.getData() != null){
			if(intent.getData().getScheme().equalsIgnoreCase("asvn")){
				if(intent.getData().getHost().equalsIgnoreCase("details")){
					packageName = intent.getData().getQueryParameter("id");
					accessToken = Utils.getStaticAccessToken(this);
					bgTask.getFullItemInfo(accessToken, "", packageName, "ads");
					aq.id(R.id.broker_prg).visible();
					aq.id(R.id.broker_retry).gone();
					aq.id(R.id.empty_view).gone();
				}
			} else {
				String param = intent.getData().getLastPathSegment();
				if (param.equalsIgnoreCase("details")) {
					packageName = intent.getData().getQueryParameter("id");
					accessToken = Utils.getStaticAccessToken(this);
					bgTask.getFullItemInfo(accessToken, "", packageName, "ads");
					aq.id(R.id.broker_prg).visible();
					aq.id(R.id.broker_retry).gone();
					aq.id(R.id.empty_view).gone();
				} else {
					aq.id(R.id.broker_prg).gone();
					aq.id(R.id.empty_view).visible();
					aq.id(R.id.broker_retry).gone();
				}
			}
		}
	}

	public void onEventMainThread(GetFullItemInfoSuccessEvent event) {
		Intent intent = new Intent(this, DetailsActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constants.ITEM_INFO, event.getItem());
		bundle.putString(Constants.PREVIOUS_SCREEN, "external");
		bundle.putInt(Constants.AUTO_INSTALL, 0);
		bundle.putString(Constants.KEY_REFERER, "ads");
		bundle.putString(Constants.STORE_KEY, "apps");
		bundle.putInt(Constants.FROM, 3);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
	}

	public void onEventMainThread(RequestFailEvent event) {
		ErrorHandler.getInstance().setContext(this)
				.handleError(event.getErrorCode());
		aq.id(R.id.broker_prg).gone();
		aq.id(R.id.broker_retry).visible();
		aq.id(R.id.empty_view).gone();
	}

	public void onEventMainThread(GetFullItemInfoFail e) {
		aq.id(R.id.broker_prg).gone();
		aq.id(R.id.broker_retry).gone();
		aq.id(R.id.empty_view).visible();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		bgTask.getFullItemInfo(accessToken, "", packageName, "ads");
		aq.id(R.id.broker_prg).visible();
		aq.id(R.id.broker_retry).gone();
		aq.id(R.id.empty_view).gone();
	}
}
